const db = require("../../config/db");
module.exports = {
    addBanner(banner_id, banner_href, banner_picUrl, banner_title, banner_create_time, banner_desc) {
        return new Promise((resolve, reject) => {
            let banner_insert_sql = `insert into news_banner(banner_id,banner_href,banner_picUrl,banner_title,banner_create_time,banner_desc) values("${banner_id}","${banner_href}","${banner_picUrl}","${banner_title}","${banner_create_time}","${banner_desc}")`;
            console.log(banner_insert_sql);

            db.query(banner_insert_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加轮播图失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "添加轮播图成功。",
                        banner_title,
                        banner_picUrl,
                    });
                }
            });
        });
    },
    bannerList() {
        return new Promise((resolve, reject) => {
            // 分页数据
            let sql = "select * from news_banner";
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: "查询轮播图失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取全部列表成功。",
                        result: rows,
                    });
                }
            });
        });
    },
    bannerPaging(page_num = 1, page_size = 5) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let size = page_size;
            // 分页数据
            let sql = "select * from news_banner limit " + startIndex + "," + size + "";
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: "查询轮播图失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取分页列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        },
                    });
                }
            });
        });
    },
    total() {
        return new Promise((resolve, reject) => {
            // 编写统计轮播图人数方法
            db.query("select count(*) as total from news_banner", function (error, among) {
                if (error) {
                    reject({
                        code: -1,
                        msg: "统计轮播图数量失败。",
                        error,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取轮播图总人数成功。",
                        total: among[0]["total"],
                    });
                }
            });
        });
    },
    deleteBanner(banner_id) {
        return new Promise((resolve, reject) => {
            let banner_check_sql = `select * from  news_banner where banner_id = "${banner_id}"`;
            console.log(banner_check_sql);
            db.query(banner_check_sql, (error, result) => {
                if (error) {
                    reject({
                        code: -1,
                        msg: "查询轮播图失败0。",
                        error,
                    });
                } else {
                    if (result.length == 1) {
                        let banner_remove_sql = `delete from  news_banner where banner_id = "${banner_id}"`;
                        db.query(banner_remove_sql, (err, rows) => {
                            if (err) {
                                reject({
                                    code: -1,
                                    msg: "删除轮播图失败1。",
                                    err,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "删除轮播图成功。",
                                });
                            }
                        });
                    } else {
                        reject({
                            code: -1,
                            msg: "删除轮播图失败2。",
                            err,
                        });
                    }
                }
            });
        });
    },
    searchBanner(keyword) {
        // console.log('1.0',{keyword});
        return new Promise((resolve, reject) => {
            // 根据db_banner的user_acc和user_nick_name字段进行关键字查询（模糊查询）
            let user_select_sql = "select * from news_banner where banner_title LIKE CONCAT('%" + keyword + "%') or banner_desc LIKE CONCAT('%" + keyword + "%')";
            db.query(user_select_sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: "搜索轮播图失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "搜索轮播图成功。",
                        result: rows,
                    });
                }
            });
        });
    },
};
